package cn.maidouya.org.service.impl;

import cn.maidouya.basic.service.impl.BaseServiceImpl;
import cn.maidouya.basic.util.MD5Utils;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.basic.util.StrUtils;
import cn.maidouya.org.domain.Employee;
import cn.maidouya.org.mapper.EmployeeMapper;
import cn.maidouya.org.query.EmployeeQuery;
import cn.maidouya.org.service.EmployeeService;
import cn.maidouya.user.domain.Logininfo;
import cn.maidouya.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    @Override
    @Transactional// 开启批量删除事件
    public void batchDel(Long[] ids) {
        employeeMapper.batchDel(ids);
    }

    @Override
    public void save(Employee employee) {
        // 给添加管理员salt加盐
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        // 给密码加盐加密
        String md5Ped = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5Ped);
        // 创建logininfo赋值
        Logininfo logininfo = new Logininfo();
        // 使用Springboot提供beanUtils拷贝
        BeanUtils.copyProperties(employee,logininfo);
        // 独有type类型修改  0管理员或者店铺
        logininfo.setType(0);
        // 添加到数据库表中去
        logininfoMapper.save(logininfo);
        // logininfo的id给employee作为关联表
        employee.setLogininfo_id(logininfo.getId());
        // 添加到数据库中去
        employeeMapper.save(employee);
    }
}